package cn.terminus.biz.customer.custombusiness;

import java.util.Date;

import cn.terminus.common.customer.bean.custombusiness.dto.CustomBusinessNewDto;
import cn.terminus.common.customer.bean.custombusiness.dto.CustomBusinessEditDto;
import cn.terminus.common.customer.bean.custombusiness.dto.CustomBusinessQueryDto;
import cn.terminus.common.customer.bean.custombusiness.dto.CustomBusinessDeleteDto;
import cn.terminus.common.customer.bean.custombusiness.dto.CustomBusinessGetsDto;
import cn.terminus.common.customer.bean.custombusiness.dto.CustomBusinessResultDto;
import cn.terminus.data.customer.custombusiness.dao.CustomBusinessDao;
import cn.terminus.result.common.exception.DataAccessException;
import cn.terminus.biz.customer.common.AbstractTerminusManager;
import org.springframework.stereotype.Repository;
import cn.terminus.result.common.result.TerminusResult;
import cn.terminus.result.common.page.TerminusPageAble;
import cn.terminus.result.common.page.TerminusPageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import javax.annotation.Resource;

import cn.terminus.result.common.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * (CustomBusiness)表服务实现类
 *
 * @author makejava
 * @since 2021-07-08 11:18:38
 */
@Component
public class CustomBusinessManager extends AbstractTerminusManager {
    @Autowired
    private CustomBusinessDao customBusinessDao;

    public Integer add(CustomBusinessNewDto dto) throws DataAccessException {
        try {
            return customBusinessDao.add(dto);
        } catch (Exception e) {
            logger.error("CustomBusinessManager新增失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public Integer update(CustomBusinessEditDto dto) throws DataAccessException {
        try {
            return customBusinessDao.update(dto);
        } catch (Exception e) {
            logger.error("CustomBusinessManager更新失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public List<CustomBusinessResultDto> gets(CustomBusinessGetsDto dto) throws DataAccessException {
        try {
            return customBusinessDao.gets(dto);
        } catch (Exception e) {
            logger.error("CustomBusinessManager查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public List<CustomBusinessResultDto> query(CustomBusinessQueryDto dto) throws DataAccessException {
        try {
            return customBusinessDao.query(dto);
        } catch (Exception e) {
            logger.error("CustomBusinessManager查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public Integer delete(CustomBusinessDeleteDto dto) throws DataAccessException {
        try {
            return customBusinessDao.delete(dto);
        } catch (Exception e) {
            logger.error("CustomBusinessManager删除失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public TerminusPageAble<CustomBusinessResultDto> page(CustomBusinessQueryDto dto, TerminusPageInfo page) throws DataAccessException {
        try {
            PageHelper.startPage(page.getCurrentPage(), page.getPageSize());
            Page<CustomBusinessResultDto> result = (Page<CustomBusinessResultDto>) customBusinessDao.query(dto);
            return new TerminusPageAble<>(BeanCopyUtils.copy(result.getResult(), CustomBusinessResultDto.class), page, result.getTotal());
        } catch (Exception e) {
            logger.error("CustomBusinessManager分页查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }
}
